package cc.vv.btpartyjob.partyservice.activity

import android.content.Intent
import android.os.Message
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.adapter.ResourceAdapter
import cc.vv.btpartyjob.partyservice.binder.ServiceBaseBinder
import cc.vv.btpartyjob.partyservice.delegate.ResourceListActivityDelegate
import cc.vv.btpartyjob.partyservice.module.bean.ResourceBean
import cc.vv.btpartyjob.partyservice.module.resp.ResourceResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 资源清单
 * Created by Chen.qi on 2018/10/29.
 */
class ResourceListActivity : BaseActivityMVP<ResourceListActivityDelegate>(),
        BaseQuickAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    /**
     * 适配器
     */
    private var mAdapter: ResourceAdapter? = null

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 开始时间
     */
    private var startTime: Long = 0

    /**
     * 结束时间
     */
    private var endTime: Long = 0

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<ResourceBean>? = null

    /**
     * 街道id
     */
    private var streetId: String? = null

    /**
     * 社区id
     */
    private var comminitId: String? = null

    /**
     * 网格Id
     */
    private var gridId: String? = null


    override fun initData() {
        viewDelegate?.setTopBarTitle("资源清单")
        viewDelegate?.showZuZhiSearch(true)
        mDataList = ArrayList()
        if (mAdapter == null) {
            mAdapter = ResourceAdapter(R.layout.item_resource_list)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
        DataPartyServer.getResourceList(streetId, comminitId, gridId, pageNo, pageSize, true)
    }

    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                if (viewDelegate?.getSearchShow()!!) {
                    viewDelegate?.showSearchView(false)
                } else {
                    viewDelegate?.showSearchView(true)
                }
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //组织查询
            JniStartActivity.startIntentOrganActivity(this, 3, 0, 100)

        }, R.id.iv_arlS_zuZhiSearch)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //空事件
            viewDelegate?.showSearchView(false)
        }, R.id.rl_arlS_searchTop)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //开始时间
            ResourceChooseServer.chooseData(this, handler, 20)
        }, R.id.ll_arlS_start)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //结束时间
            ResourceChooseServer.chooseData(this, handler, 21)
        }, R.id.ll_arlS_end)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //搜索
            closeInput()
            val title = viewDelegate?.getInputSearchText()
            pageNo = 1
            DataPartyServer.getResourceSearchList(pageNo, pageSize, title!!, startTime, endTime, true)
            viewDelegate?.showSearchView(false)
        }, R.id.tv_arlS_searchResource)

    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            20 -> {
                //开始时间
                val startString: String = msg.obj as String
                startTime = LKTimeUtil.getInstance().getLongTime(startString, "yyyy-MM-dd")
                viewDelegate?.setStartTime(startString)
            }
            21 -> {
                //结束时间
                val endString: String = msg.obj as String
                endTime = LKTimeUtil.getInstance().getLongTime(endString, "yyyy-MM-dd")
                viewDelegate?.setEndTime(endString)
            }
        }
    }

    override fun getDelegateClass(): Class<ResourceListActivityDelegate> {
        return ResourceListActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ServiceBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        JniStartActivity.startIntentWebActivity(this, H5UrlEnum.资源清单列表详情, mDataList?.get(position)?.id, "资源清单详情")
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is ResourceResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                    if (mDataList!!.size < 10) {
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    }
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        DataPartyServer.getResourceList(streetId, comminitId, gridId,pageNo, pageSize, false)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        DataPartyServer.getResourceList(streetId, comminitId, gridId,pageNo, pageSize, false)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            when (type) {
                "0" -> {
                    streetId = ""
                    comminitId = ""
                    gridId = ""
                }
                "1" -> {
                    streetId = orgId
                    comminitId = ""
                    gridId = ""
                }
                "2" -> {
                    streetId = ""
                    comminitId = orgId
                    gridId = ""
                }
                "3" -> {
                    streetId = ""
                    comminitId = ""
                    gridId = orgId
                }
            }
            pageNo = 1
            DataPartyServer.getResourceList(streetId, comminitId, gridId,pageNo, pageSize, false)
        }
    }
}